import TreeNode from "../common/tree_node";
//思路:queue一层一层的装。装完再弄下一层
function sortedArrayToBST(nums: number[]): TreeNode | null {
  if(nums.length == 0) return null

  let root = new TreeNode(nums.shift())
  let queue = [root]
  while(nums.length > 0&&queue.length >0) {
    let tmp =  queue.shift() as TreeNode
    let left = nums.shift()
    let right = nums.shift()
    if(left != undefined) {
      let leftNode = new TreeNode(left)
      tmp.left = leftNode
      queue.push(leftNode)
    }
    if(right != undefined) {
      let rightNode = new TreeNode(right)
      tmp.right = rightNode
      queue.push(rightNode)
    }
    
  }

  return root

};
console.log(sortedArrayToBST([-10,-3,0,5,9]))